<?php
/**
 * @package: proiect_battles
 * @author: master (mic_programator)
 * @date: 2/6/2012
 * @copyright: micul-programator.ro
 * @name: login.php
 */
//includem fisierele de limba
 //--nu e setata nici o sesiune, nici un config general, deci nu stiu ce limba va alege si cum o va alege userul

//includem funciile generale

if(!defined('ANTIHACK')){ die("Acces nepermis");}

if(!file_exists("config.php")){
    die('Nu exista fisierul config.php');
}
else{
    include_once("config.php");
}





//creem clasa de lucru 
class Content{
    /**
     * Preia din baza de date utilizatorul care se presupune ca ar fi autor
     * @param array $date
     * @return bool- true or false 
     */
    function get_autor($date){
    //compunem query
    $query="SELECT * FROM ".TABEL_USERI." WHERE name='{$date['autor']}'";
    //rulam query
    $rez=mysql_query($query) ;
    //verificam daca avem user
    if(mysql_num_rows($rez)==1){
        return true;
    }
    return false;
    }
    
    /**
     * Introduce un anunt in baza de date
     * @param array $date - datele anuntului
     * @return bool -true or false 
     */
    function insert_anunt($date){
        $data_publicari=$date['an'].'-'.$date['luna'].'-'.$date['zi'];
        $data_expirari=$date['an_ex'].'-'.$date['luna_ex'].'-'.$date['zi_ex'];
        
        if(($date['zi_ex']==0)&($date['luna_ex']==0)&($date['an_ex']==0)){
             $data_expirari=($date['an']+100).'-'.$date['luna'].'-'.$date['zi'];
        }
        //compunem query
        $query="INSERT INTO ".TABEL_ANUNT." (titlu,autor,continut,data_publicare,data_end) 
                VALUES('{$date['title']}','{$date['autor']}','{$date['content']}','{$data_publicari}','{$data_expirari}')";
        //rulam query
                $rez=mysql_query($query);
        //verificam daca sa facut insertul
                if(mysql_affected_rows()==1){
                    return true;
                }
                return false;
    }
    /**
     * Preia lista anunturilor
     * @return array $anunturi - lista anunturilor
     */
    function get_anunturi($limit=100){
        $annturi=array();
        //compunem query
        
        $query="SELECT * FROM ".TABEL_ANUNT." WHERE CURDATE()>= data_publicare AND CURDATE()<= data_end limit 0,{$limit} ";
        //rulam query
        $rez=mysql_query($query);
        
        while ($row = mysql_fetch_array($rez)) {
            $anunturi[$row['id']]=$row;
        }
        
        return $anunturi;
    }
    
    function get_art_for_id($art){
        //compunem query
        $query="SELECT * FROM ".TABEL_ANUNT." WHERE id={$art}";
        //rulam query
        $rez=mysql_query($query);
        $rez=mysql_fetch_array($rez);
        //returnam rez
        return $rez;
        
    }
}

/*partea de controler*/
//actiuni posibile
/*
 * add_new_art      -adauga un nou anunt
 * del_art          -sterge un anunt
 * edit_art         -sterge un anunt
 * list_art         -afiseaza o lista cu ultimele 10 anunturi
 * show_art         -afiseaza un anunt 
 */


//preluam actiunea
$actiune=(isset ($_GET['action']))?$_GET['action']:'list_art';

//start switch

switch($actiune){
    
   case 'add_new_art': 
       //seo
       $tpl_dc['title']='Adauga un nou anunt';
       $tpl_dc['description']='Adauga un nou anunt';
       $tpl_dc['keywords']='adauga,anunt,nou';
       //daca nu s-au trimis date
       if(!isset($_POST['trimite'])){
           //afisam formularul de scriere al anuntului
           $tpl_dc['content']=show_form_add_new_art();
           //break
           break;
       }
       //preluam datele
       $date=get_add_new_art();
       //verificam de erori
       $erori=check_add_new_art($date);
       //daca sunt erori
       if(count($erori)){
          //se afiseaza erorile
           $tpl_dc['erori']=$erori;
           //afisam formularul
           $tpl_dc['content']=show_form_add_new_art();
           break;
       }
       //introducem anuntul
       $rez=Content::insert_anunt($date);
       if(!$rez){
           $tpl_dc['erori'][]='Eroare: Anuntul nu poate fi salvat,reincearca';
           //afisam formularul
           $tpl_dc['content']=show_form_add_new_art();
           break;
       }
       $tpl_dc['mesaje'][]='Anuntul a fost salvat';
       break;
       
       default:
      case 'list_art':
          
        //preluam datele din db
          $date=Content::get_anunturi();
          //daca nu sunt anunturi
          if(!count($date)){
              //afisam mesaj
              $tpl_dc['mesaje'][]='Nu sunt mesaje';
             break;
          }
          //afisam lista de anunturi
          $tpl_dc['content']=show_list_art($date);
          
      break;
      
      case 'show_art':
          
          //seo
          
          //daca nu se preia din get
          
          if(!isset($_GET['id'])){
              $tpl_dc['mesaje'][]='Nu ai specificat anuntul ';
              break;
          }
          //preluam id-ul anuntului
          $art=intval($_GET['id']);
          
          //verificam daca exista acest articol in baza de date
          $ex=Content::get_art_for_id($art);
          //daca nu exista articolul
          if(!$ex){
              //afisam mesaj
              $tpl_dc['mesaje'][]='Acest anunt nu exista';
              break;
          }
          
          //afiseaza articolul specificat
          $tpl_dc['content']=show_art($ex);
          
          
          break;
}

//functii contet
/**
 * Afiseaza formularul de scriere al anuntului
 * @return string $afiseaza
 * 
 */
function show_form_add_new_art(){
   $date=get_add_new_art();
    
    $an_curent=date('Y');
    $luna_curenta=date('n');
    $zi_curenta=date('j');
     
    $sel_zi='<select name="zi"><option value="0">zi</option>';
    
     for($i=1;$i<32;$i++){
         if($date['zi']==''){
             
             if($zi_curenta==$i){
                 $sel_zi.='<option value="'.$i.'" selected="selected">'.$i.'</option>'; 
             }
             else{
                 $sel_zi.='<option value="'.$i.'">'.$i.'</option>';  
             }
         }else{
             
            if($date['zi']==$i){   
            $sel_zi.='<option value="'.$i.'" selected="selected">'.$i.'</option>';  
            }
            else{
            $sel_zi.='<option value="'.$i.'">'.$i.'</option>';   
            } 
         }
        
    }
    $sel_zi.='</select>';
    //facem selectul pentru luna
    $sel_luna='<select name="luna"><option value="0">luna</option>';
    
    for($i=1;$i<13;$i++){
        
        if($date['luna']==''){
           if($luna_curenta==$i){
                  $sel_luna.='<option value="'.$i.'" selected="selected">'.$i.'</option>'; 
             }
             else{
                  $sel_luna.='<option value="'.$i.'">'.$i.'</option>';  
             } 
            
        }else
        {
            if($date['luna']==$i){
                $sel_luna.='<option value="'.$i.'" selected="selected">'.$i.'</option>'; 
            }
             else {
                $sel_luna.='<option value="'.$i.'">'.$i.'</option>'; 
            }
        }
    }
    $sel_luna.='</select>';
    //facem selectul pentru an
    $an_curent=date('Y');
    
    $sel_an='<select name="an"><option value="0">an</option>';
        if($date['an']==''){
            $sel_an.= '<option value="'.$an_curent.'" selected="selected">'.$an_curent.'</option>';
        }
        else{
           if($an_curent==$date['an']){
            $sel_an.= '<option value="'.$an_curent.'" selected="selected">'.$an_curent.'</option>';
        }  else {
             $sel_an.= '<option value="'.$an_curent.'">'.$an_curent.'</option>';
        } 
        }
        
      
    
    $sel_an.='</select>';
    
    
     $sel_zi_ex='<select name="zi_ex"><option value="0">zi</option>';
    
     for($i=1;$i<32;$i++){
            if($date['zi_ex']==$i){   
            $sel_zi_ex.='<option value="'.$i.'" selected="selected">'.$i.'</option>';  
            }
            else{
            $sel_zi_ex.='<option value="'.$i.'">'.$i.'</option>';   
            } 
         
        
    }
    $sel_zi_ex.='</select>';
    //facem selectul pentru luna
    $sel_luna_ex='<select name="luna_ex"><option value="0">luna</option>';
    
    for($i=1;$i<13;$i++){
        
        
            if($date['luna_ex']==$i){
                $sel_luna_ex.='<option value="'.$i.'" selected="selected">'.$i.'</option>'; 
            }
             else {
                 $sel_luna_ex.='<option value="'.$i.'">'.$i.'</option>'; 
            }
        
    }
     $sel_luna_ex.='</select>';
    //facem selectul pentru an
    $an_curent=date('Y');
    $an_viitor=$an_curent+4;
    $sel_an_ex='<select name="an_ex"><option value="0">an</option>';
    
        for($i=$an_curent;$i<$an_viitor;$i++){
            if($date['an_ex']==$i)
             $sel_an_ex.= '<option value="'.$i.'"  selected="selected">'.$i.'</option>';   
            else
            $sel_an_ex.= '<option value="'.$i.'">'.$i.'</option>';
        }
    $sel_an_ex.='</select>';
    
    $afiseaza='<form method="post" action="logged.php?display=anunt&action=add_new_art">
        <table width="60%" align="center">
  <tr>
    <th colspan="2" scope="col">Adauga un nou anunt</th>
  </tr>
  <tr>
    <td colspan="2" align="center">
      <textarea name="content"  cols="80" rows="15">'.$date['title'].'</textarea>
    </td>
  </tr>
  <tr>
    <td width="50%" height="27" align="right">Titlu Anunt:</td>
    <td width="50%" align="left">
        <input type="text" name="titlu" value="'.$date['content'].'" />
   </td>
  </tr>
  <tr>
    <td align="right">Autor:</td>
    <td align="left"><input type="text" name="autor" value="'.$date['autor'].'" /></td>
  </tr>
  <tr>
    <td align="right">Data Publicari:</td>
    <td align="left">'.$sel_zi.'-'.$sel_luna.'-'.$sel_an.'</td>
  </tr>
  <tr>
    <td align="right">Data Expirari(lasati neselectat pentru "forever"):</td>
    <td align="left">'.$sel_zi_ex.'-'.$sel_luna_ex.'-'.$sel_an_ex.'</td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input type="submit" name="trimite"  value="Salveaza" /></td>
  </tr>
</table></form>';
    
    return $afiseaza;
}

/**
 * preia datele pentru crearea unui nou anunt
 * @return array - $date
 */
function get_add_new_art(){
    $date=array();
    $date['content']=isset($_POST['content'])?nl2br(strip_tags($_POST['content'])):'';
    $date['autor']=isset($_POST['autor'])?clean($_POST['autor']):'';
    $date['title']=isset($_POST['titlu'])?clean($_POST['titlu']):'';
    $date['zi']=isset($_POST['zi'])?clean($_POST['zi']):'';
    $date['luna']=isset($_POST['luna'])?clean($_POST['luna']):'';
    $date['an']=isset($_POST['an'])?clean($_POST['an']):'';
    $date['zi_ex']=isset($_POST['zi_ex'])?clean($_POST['zi_ex']):'';
    $date['luna_ex']=isset($_POST['luna_ex'])?clean($_POST['luna_ex']):'';
    $date['an_ex']=isset($_POST['an_ex'])?clean($_POST['an_ex']):'';
    return $date;
}


/**
 *  Verifica erorile aparute in formularul de creare al unui anunt
 * @param array $date - datele primite din post
 * @return array $erori - erorile returnate
 */
function check_add_new_art($date){
    $data_publicari=strtotime($date['an'].'-'.$date['luna'].'-'.$date['zi']);
    $data_expirari=strtotime($date['an_ex'].'-'.$date['luna_ex'].'-'.$date['zi_ex']);
    //$data_curenta=strtotime(date("Y-n-j"));
    $data_curenta=strtotime(date("Y-m-d"));
    $erori=array();
    if(!Content::get_autor($date)){
        $erori[]='Autorul nu exista';
    }
    if($data_publicari<$data_curenta){
        $erori[]='Data publicarii nu poate fi setata in trecut';
    }
    if(($date['zi_ex']!=0)&($date['luna_ex']!=0)&($date['an_ex']!=0)){
        if($data_expirari<$data_publicari){
            $erori[]='Data epirari nu poata fi setata in trecutul publicari';
        }
        if(!checkdate($date['luna_ex'],$date['zi_ex'],$date['an_ex'])){
        $erori[]='Data expirari este incorecta';
         }
    }
    if(empty($date['content'])){
        $erori[]='Nu ai introdus nici un anunt';
    }
    if(empty($date['title'])){
         $erori[]='Nu ai introdus titlul';
    }
    if(!checkdate($date['luna'],$date['zi'],$date['an'])){
        $erori[]='Data publicari este incorecta';
    }
    
    return $erori;
   
}

function show_list_art($date){
    $afiseaza='<br><center><table width="95%" border="0" id="art">
  <tr>
    <th width="83%" scope="col">Anunt</th>
    <th width="17%" scope="col">Obtiuni</th>
  </tr>';
    
    foreach ($date as $anunt){
  $afiseaza.='<tr>
    <td height="24" bgcolor="#CCCCCC">'.$anunt['titlu'].'.</td>
    <td bgcolor="#CCCCCC">[e]'.'<a href="logged.php?display=anunt&amp;action=show_art&amp;id='.$anunt['id'].'" title="Vizitati Anuntul">[v]</a>'.'[s]</td>
  </tr>';
    }
    $afiseaza.='
    </table></center>';
    return $afiseaza;
}

function show_art($date){
    
    $afiseaza='<center><br><table width="95%" border="0" id="art">
  <tr>
    <th colspan="2" bgcolor="#CCCCCC" scope="col">'.$date['titlu'].'</th>
  </tr>
  <tr>
    <td colspan="2" id="continut" bgcolor="#CCCCCC">'.$date['continut'].'</td>
  </tr>
  <tr>
    <td align="left" bgcolor="#CCCCCC">Autor:'.$date['autor'].'</td>
    <td align="left" bgcolor="#CCCCCC">Obtiuni:</td>
  </tr>
</table></center>';
    
    return $afiseaza;
}

//partea de template


if(count($tpl_dc['erori'])){
    foreach($tpl_dc['erori'] as $erori){
   $tpl_dc['content'].='<font color="red">'.$erori.'</font><br>';
    }
}

if(count($tpl_dc['mesaje'])){
    
    foreach($tpl_dc['mesaje'] as $msg){
   $tpl_dc['content'].= $msg.'<br>';
    }
}

?>
